package dcp.bss.modules.bussiness.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import dcp.bss.common.model.KeyValueBean;
import dcp.bss.common.util.DBUtil;
import dcp.bss.common.util.DateUtils;
import dcp.bss.common.util.R;
import dcp.bss.modules.bussiness.entity.LockStatusEntity;
import dcp.bss.modules.bussiness.service.LockStatusService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;


/**
 * 车辆表
 *
 * @author caochaofeng
 * @email caochaofeng@dacanginfo.com
 * @date 2018-08-19 17:21:27
 */
@RestController
@RequestMapping("miniapp")
@Slf4j
public class MiniAppController {
    @Autowired
    private LockStatusService lockStatusService;
    /**
     * 列表  https://www.jhyj56.com/ztjk/miniapp/carrier
     */
    @RequestMapping("/carrier")
    public R list() throws Exception {
        R r = new R();
        String  sql ="SELECT\n" +
                "\tcarrier_name,\n" +
                "\tcount( 1 ) AS '全部',\n" +
                "\tcount( IF ( gps_time > date_sub( curdate( ), INTERVAL 1 hour ), 1, NULL ) ) AS '在线',\n" +
                "\tcount( IF ( gps_time < date_sub( curdate( ), INTERVAL 1 hour ), 1, NULL ) ) AS '离线' \n" +
                "FROM\n" +
                "\t`biz_lock_status` \n" +
                "GROUP BY\n" +
                "\tcarrier_name";
        List<List<Object>> dataList =  DBUtil.executeQueryFH(sql);
        r.put("data", dataList);
        return r;
    }

    /**
     * 列表 https://www.jhyj56.com/ztjk/miniapp/deviceList
     */
    @RequestMapping("/deviceList")
    public R deviceList(@RequestParam(value="carrierName",required = true) String carrierName, Integer status) throws Exception {
        R r = new R();
        Wrapper<LockStatusEntity> ew =    new EntityWrapper<LockStatusEntity>().eq("carrier_name",carrierName);
        if(status!=null) {
            //离线
            if (status == 0) {
                ew.le("gps_time", DateUtils.formatDateTime(DateUtils.getMoveDate(0,0,-1)));
            } else {
                ew.gt("gps_time", DateUtils.formatDateTime(DateUtils.getMoveDate(0,0,-1)));
            }
        }
        List<LockStatusEntity> dataList =    lockStatusService.selectList(ew);
        r.put("data", dataList);
        return r;
    }



}
